package com.seemygo.shop.cloud.core;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/{uuid}")
@Component
public class WebSocketServer {


    /**
     * 当有客户端建立连接时进入此方法
     * @param uuid
     * @param session
     */
    @OnOpen
    public void onOpen(@PathParam("uuid") String uuid, Session session){
        System.out.println("客户端：" + uuid + "连接上来了");
        WebSocketSessionManager.INSTANCE.put(uuid, session);

    }

    @OnMessage
    public void onClose(@PathParam("uuid") String uuid, String message){
        System.out.println("收到客户端：" + uuid + "的消息" + message);

    }

    @OnClose
    public void onClose(@PathParam("uuid") String uuid, Session session){
        System.out.println("客户端：" + uuid + "关闭连接了");
        if(!StringUtils.isEmpty(uuid)){
            WebSocketSessionManager.INSTANCE.remove(uuid);
        }


    }

    @OnError
    public void OnError(@PathParam("uuid") String uuid, Throwable throwable){
        System.out.println("客户端：" + uuid + "出现异常了");
        throwable.printStackTrace();

    }
}
